package com.zzh.custom;

import android.content.Context;
import android.text.Layout;
import android.text.method.ScrollingMovementMethod;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewParent;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatTextView;

/**
 * @author zhangzhihao
 * @email zhangzhihao@gongjiangren.net
 * @date 2020-09-21
 * @desc
 */
public class ScrollTextView extends AppCompatTextView {

    public ScrollTextView(Context context) {
        this(context, null);
    }

    public ScrollTextView(Context context, AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public ScrollTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        setMovementMethod(ScrollingMovementMethod.getInstance());
        setOnTouchListener(new MyOnTouchListener(null));
    }

    @Override
    public void setOnTouchListener(OnTouchListener l) {
        if (l != null && l instanceof MyOnTouchListener) {
            super.setOnTouchListener(l);
            return;
        }
        super.setOnTouchListener(new MyOnTouchListener(l));
    }

    private static class MyOnTouchListener implements OnTouchListener {
        private OnTouchListener onTouchListener;

        public MyOnTouchListener(OnTouchListener onTouchListener) {
            this.onTouchListener = onTouchListener;
        }

        int scrollHeight = 0;
        boolean disallowIntercept = true;
        float downY = 0;
        ViewParent viewParent;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            TextView textView = (TextView) v;
            if (textView.getLineCount() <= textView.getMaxLines()) {
                v.getParent().requestDisallowInterceptTouchEvent(false);
                if (onTouchListener != null) {
                    return onTouchListener.onTouch(v, event);
                }
                return false;
            }
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    scrollHeight = getTextViewHeight(textView) - v.getHeight();
                    disallowIntercept = true;
                    downY = event.getY();
                    viewParent = v.getParent();
                    viewParent.requestDisallowInterceptTouchEvent(disallowIntercept);
                    break;
                case MotionEvent.ACTION_MOVE:
                    if (downY > event.getY()) {
                        disallowIntercept = scrollHeight != v.getScrollY();
                    } else {
                        disallowIntercept = 0 != v.getScrollY();
                    }
                    viewParent.requestDisallowInterceptTouchEvent(disallowIntercept);
                    break;
                default:
                    viewParent.requestDisallowInterceptTouchEvent(false);
                    break;
            }
            if (onTouchListener != null) {
                return onTouchListener.onTouch(v, event);
            }
            return false;
        }

        private int getTextViewHeight(TextView textView) {
            Layout layout = textView.getLayout();
            int desired = layout.getLineTop(textView.getLineCount());
            int padding = textView.getCompoundPaddingTop() +
                    textView.getCompoundPaddingBottom();
            return desired + padding;
        }
    }


}
